//
//  SEGDailySubjectViewModel.m
//  Commom
//
//  Created by 梁学彰 on 2020/6/15.
//

#import "SEGDailySubjectViewModel.h"

@implementation SEGDailySubjectViewModel
- (instancetype)init {
    if (self = [super init]) {
        [self initData];
        [self seg_createDataCommand];
    }
    return self;
}

- (void)initData {
    self.dataSource = [NSArray array];
    self.pageNo = 1;
    self.totalPages = 1;
}
#pragma mark - send request
- (void)seg_createDataCommand {
    @weakify(self);
    self.dataCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            @strongify(self);
            if (self.pageNo > self.totalPages) {
                [subscriber sendCompleted];
                return nil;
            }
            __weak typeof (self) weakSelf = self;
            [self.sharedNewManager requestDailySubjectArrayWithPageNo:[NSString stringWithFormat:@"%ld",(unsigned long)self.pageNo] pageLimit:@"10" finished:^(BOOL isSuccessed, NSString *codeType, NSString *errorMessage) {
                if (isSuccessed) {
                    weakSelf.dataSource = weakSelf.sharedNewManager.recommendSubjectArray;
                    [subscriber sendNext:nil];
                }else {
                    [subscriber sendError:[NSError errorWithDomain:NSURLErrorDomain code:100 userInfo:@{NSLocalizedDescriptionKey:errorMessage}]];
                }
                [subscriber sendCompleted];
            }];
            return nil;
        }];
    }];
    self.dataCommand.allowsConcurrentExecution = YES;
}

#pragma mark - getter

- (SEGSharedNewManager *)sharedNewManager {
    if (!_sharedNewManager) {
        _sharedNewManager = [[SEGSharedNewManager alloc] init];
    }
    return _sharedNewManager;
}


@end
